package com.hnou.boxuegu.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hnou.boxuegu.R;
import com.hnou.boxuegu.utils.UtilsHelper;
import com.hnou.boxuegu.view.MyInfoView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FrameLayout mBodyLayout; // 主体部分
    private LinearLayout mBottomLayout; // 底部导航栏
    private RelativeLayout mCourseBtn,mExerciseBtn, mMyInfoBtn;  // 导航按钮组件
    private TextView tv_course, tv_exercise,tv_myinfo;
    private ImageView iv_course, iv_exercise,iv_myinfo;
    private TextView tv_back,tv_main_title;
    private RelativeLayout rl_title_bar;
    private MyInfoView mMyInfoView;
    //private MyCourseView mCourseView,mExerciseView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        setListener();
        selectDisplayView(0);// 默认选择0号，课程视图进行显示

//FIXME        临时使用，LoginActivity Activity
        Intent intent = new Intent(MainActivity.this,LoginActivity.class);
        startActivity(intent);
    }

    protected  long extTime = 0; // 上一次按下back建的时间
    /*
    * 监听键盘事件
    * */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if(System.currentTimeMillis() - extTime > 2000){
                Toast.makeText(this, "再按一次退出博学谷", Toast.LENGTH_SHORT).show();
                extTime = System.currentTimeMillis();
            }else{
                MainActivity.this.finish();
                if(UtilsHelper.readLoginStatus(this)){
                    UtilsHelper.clearLoginStatus(this);
                }
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    private void setListener() {
        mCourseBtn.setOnClickListener(this);
        mExerciseBtn.setOnClickListener(this);
        mMyInfoBtn.setOnClickListener(this);
    }

    /*
    * 获取界面上的控件
    * */
    private void init() {
        tv_back = findViewById(R.id.tv_back);
        tv_main_title = findViewById(R.id.tv_main_title);
        rl_title_bar = findViewById(R.id.title_bar);
        mBodyLayout = findViewById(R.id.main_body);

        mBottomLayout = findViewById(R.id.main_bottom_bar);
        mCourseBtn = findViewById(R.id.bottom_bar_course_btn);
        mExerciseBtn = findViewById(R.id.bottom_bar_exercises_btn);
        mMyInfoBtn = findViewById(R.id.bottom_bar_myinfo_btn);
        tv_course = findViewById(R.id.bottom_bar_text_course);
        tv_exercise = findViewById(R.id.bottom_bar_text_exercises);
        tv_myinfo = findViewById(R.id.bottom_bar_text_myinfo);
        iv_course = findViewById(R.id.bottom_bar_image_course);
        iv_exercise = findViewById(R.id.bottom_bar_image_exercises);
        iv_myinfo = findViewById(R.id.bottom_bar_image_myinfo);

        rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        tv_main_title.setText("博学谷课程");
        tv_back.setVisibility(View.GONE);

    }
    /*
    清理选中状态，都设置为未选中
    * */
    private void setNotSelectedStatus(){
        // 所有文字灰色，图片灰色，选中状态未选中
        tv_course.setTextColor(Color.parseColor("#666666"));
        tv_exercise.setTextColor(Color.parseColor("#666666"));
        tv_myinfo.setTextColor(Color.parseColor("#666666"));
        iv_course.setImageResource(R.drawable.main_course_icon);
        iv_exercise.setImageResource(R.drawable.main_exercises_icon);
        iv_myinfo.setImageResource(R.drawable.main_my_icon);
        mCourseBtn.setSelected(false);
        mExerciseBtn.setSelected(false);
        mMyInfoBtn.setSelected(false);
    }
    // 将index对应的按钮设置为选中的颜色和状态
    private void setSelectedStatus(int index){
        switch (index){
            case 0:
                mCourseBtn.setSelected(true);
                iv_course.setImageResource(R.drawable.main_course_icon_selected);
                tv_course.setTextColor(Color.parseColor("#0097EF"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("博学谷课程");
                break;
            case 1:
                mExerciseBtn.setSelected(true);

                iv_exercise.setImageResource(R.drawable.main_exercises_icon_selected);
                tv_exercise.setTextColor(Color.parseColor("#0097EF"));
                rl_title_bar.setVisibility(View.VISIBLE);
                tv_main_title.setText("博学谷习题");
                break;
            case 2:
                mMyInfoBtn.setSelected(true);
                iv_myinfo.setImageResource(R.drawable.main_my_icon_selected);
                tv_myinfo.setTextColor(Color.parseColor("#0097EF"));
                rl_title_bar.setVisibility(View.GONE);
                break;

        }
    }
    /*
    创建视图
     */
    private void createView(int viewIndex){
        switch (viewIndex){
            case 0: // 课程
                break;
            case 1: // 习题
                break;
            case 2: // 个人中心
                if(mMyInfoView==null){
                    mMyInfoView =new MyInfoView(MainActivity.this);
                    View myInfoView = mMyInfoView.getView();
                    mBodyLayout.addView(myInfoView);
                }
                break;
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bottom_bar_course_btn:
                setNotSelectedStatus(); // 清理状态信息
                selectDisplayView(0);
                break;
            case R.id.bottom_bar_exercises_btn:
                setNotSelectedStatus(); // 清理状态信息
                selectDisplayView(1);
                break;
            case R.id.bottom_bar_myinfo_btn:
                setNotSelectedStatus(); // 清理状态信息
                selectDisplayView(2);
                break;
        }
    }

    private void selectDisplayView(int index){
        hideAllView(); // 隐藏主体视图
        createView(index); // 创建主体 视图
        setSelectedStatus(index); // 设置底部选中状态


    }
    //TODO 隐藏所有的视图
    private void hideAllView() {
        for (int i = 0; i < mBodyLayout.getChildCount(); i++) {
            mBodyLayout.getChildAt(i).setVisibility(View.GONE);
        }
    }
}